<%-- 
    Document   : DoneTranslation
    Created on : Dec 13, 2009, 7:24:56 PM
    Author     : ayca
--%>
<%@page contentType="text/html" pageEncoding="UTF-8" language="java" import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page import="mail.SendMail"%>

<%
String text= request.getParameter("text");
String transID = request.getParameter("transId");
String username = session.getAttribute("username").toString();

System.out.println("trans id = " +transID);

//System.out.println("text = " +text);


String DRIVER = "org.gjt.mm.mysql.Driver";
Class.forName(DRIVER).newInstance();


Connection con=null;
ResultSet rs=null;
Statement stmt=null;

try{
String url="jdbc:mysql://localhost/CoTrans?user=ayca&password=asasasas";

int i=1;
con=DriverManager.getConnection(url);
stmt=con.createStatement();

String q_path = "SELECT path FROM TranslationInfo WHERE transID='"+transID+"' ";
String q_userid = "SELECT userID,email FROM UserInfo WHERE userName='"+username+"' ";
String q_locked = "UPDATE TranslationInfo SET isLocked=0 WHERE transID='"+transID+"'";
String q_users = "SELECT UserInfo.email FROM UserInfo,UserTranslation WHERE UserInfo.userID = UserTranslation.userID AND transID='"+transID+"'";
rs=stmt.executeQuery(q_path);
String fileName = rs.getString(1);
System.out.println("file name: " + fileName);
rs.close();

rs = stmt.executeQuery(q_userid);
String userID = rs.getString(1);
String email_from = rs.getString(2);
String q_done = "UPDATE UserTranslation SET isDone=1 WHERE transID='"+transID+"' AND userID='"+userID+"'";

int success_locked = stmt.executeUpdate(q_locked);
int success_done = stmt.executeUpdate(q_done);

rs = stmt.executeQuery(q_users);
String []emails_to = {};
i=0;
while(rs.next()){
    emails_to[i] = rs.getString(1);
}

stmt.close();
con.close();


try {
    PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
    pw.println(text);
    //clean up
    pw.close();
} catch(IOException e) {
   out.println(e.getMessage());
}


String subject = "Do you think translation is done?";
String message = username + " thinks your translation with translation id " + transID + " is done. Please check it from the system!";
SendMail sm = new SendMail();
sm.SendYourMail(emails_to, subject, message, "cotransbeta@gmail.com");

}
catch(Exception e){
System.out.println(e.getMessage());
}



%>